package com.atguigu.day73_demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class  Myclock {
    private final Lock lockA = new ReentrantLock();
    private final Lock lockB = new ReentrantLock();

    public void add() {
        lockA.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "获得了A锁");
            lockB.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "获得了B锁");
                // 这里可以添加其他处理逻辑
            } finally {
                System.out.println(Thread.currentThread().getName() + "释放了B锁");
                lockB.unlock();
            }
        } finally {
            System.out.println(Thread.currentThread().getName() + "释放了A锁");
            lockA.unlock();
        }
    }

    public void desc() {
        lockB.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "获得了B锁");
            lockA.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "获得了A锁");
                // 这里可以添加其他处理逻辑
            } finally {
                System.out.println(Thread.currentThread().getName() + "释放了A锁");
                lockA.unlock();
            }
        } finally {
            System.out.println(Thread.currentThread().getName() + "释放了B锁");
            lockB.unlock();
        }
    }
}
    public class DeadLockDemo {
        public  static void main(String[] args) {
            Myclock myclock = new Myclock();
            new Thread(() -> {
                myclock.add();
            }, "A").start();

            new Thread(() -> {
                myclock.desc();
            }, "B").start();
        }

}